package com.ruoyi.web.controller.workSafetyStandardization.rule;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.pengjie.SnowflakeIdUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.workSafetyStandardization.domain.GradeLimit;
import com.ruoyi.workSafetyStandardization.domain.GradeRule;
import com.ruoyi.workSafetyStandardization.dto.grade.GradeLimitDto;
import com.ruoyi.workSafetyStandardization.dto.grade.GradeMajorRuleDto;
import com.ruoyi.workSafetyStandardization.service.IGradeLimitService;
import com.ruoyi.workSafetyStandardization.service.IGradeRuleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 评分-评级限制Controller
 *
 * @author pengjie
 * @date 2023-12-15
 */
@RestController
@RequestMapping("/secure/securerule/gradeLimit")
public class GradeLimitController extends BaseController
{
    @Autowired
    private IGradeLimitService gradeLimitService;
    @Autowired
    private IGradeRuleService gradeRuleService;

    /**
     * 查询评分-评级限制列表
     */
    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:gradeLimit:list')")
    @GetMapping("/list")
    public TableDataInfo list(GradeLimit gradeLimit, @Param("pageSize") String pageSize, @Param("pageNum") String pageNum)
    {
        PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        List<GradeLimit> list = gradeLimitService.selectGradeLimitList(gradeLimit);
        List<GradeLimitDto> listDto =list.stream().map(it->{
            GradeLimitDto Dto=new GradeLimitDto();
            BeanUtils.copyProperties(it,Dto);
            GradeRule gradeRule=gradeRuleService.selectGradeRuleByGradeRuleId(it.getGradeRuleId());
            Dto.setTargetContent(gradeRule.getTargetContent());
            return  Dto;
        }).collect(Collectors.toList());
        return getDataTable(listDto);
    }

    /**
     * 导出评分-评级限制列表
     */
    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:gradeLimit:export')")
    @Log(title = "评分-评级限制", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GradeLimit gradeLimit)
    {
        List<GradeLimit> list = gradeLimitService.selectGradeLimitList(gradeLimit);
        ExcelUtil<GradeLimit> util = new ExcelUtil<GradeLimit>(GradeLimit.class);
        util.exportExcel(response, list, "评分-评级限制数据");
    }

    /**
     * 获取评分-评级限制详细信息
     */
    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:gradeLimit:query')")
    @GetMapping(value = "/{gradeLimitId}")
    public AjaxResult getInfo(@PathVariable("gradeLimitId") String gradeLimitId)
    {
        return success(gradeLimitService.selectGradeLimitByGradeLimitId(gradeLimitId));
    }

    /**
     * 新增评分-评级限制
     */
    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:gradeLimit:add')")
    @Log(title = "评分-评级限制", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GradeLimit gradeLimit)
    {
        gradeLimit.setGradeLimitId(String.valueOf(new SnowflakeIdUtils(5,8).nextId()));
        return toAjax(gradeLimitService.insertGradeLimit(gradeLimit));
    }

    /**
     * 修改评分-评级限制
     */
    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:gradeLimit:edit')")
    @Log(title = "评分-评级限制", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GradeLimit gradeLimit)
    {
        return toAjax(gradeLimitService.updateGradeLimit(gradeLimit));
    }

    /**
     * 删除评分-评级限制
     */
    @PreAuthorize("@ss.hasPermi('workSafetyStandardization:gradeLimit:remove')")
    @Log(title = "评分-评级限制", businessType = BusinessType.DELETE)
	@DeleteMapping("/{gradeLimitIds}")
    public AjaxResult remove(@PathVariable String[] gradeLimitIds)
    {
        return toAjax(gradeLimitService.deleteGradeLimitByGradeLimitIds(gradeLimitIds));
    }
}
